package com.tgh.gles2.c3.geotest;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.tgh.utils.GLShaderManager;
import com.tgh.utils.GLShaderManager.ShaderType;
import com.tgh.utils.GLTools;
import com.tgh.utils.GLTriangleBatch;

public class MySurfaceView extends GLSurfaceView {

    public MySurfaceView(Context context) {
        super(context);
        init();
    }

    public MySurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    
    private void init() {
        setEGLContextClientVersion(2);
        setRenderer(new SceneRenderer());
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
        
    }
    private float lastX,lastY;
    private float xAngle,yAngle;
    private float factor=(float) (5*Math.PI/180);
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x=event.getX();
        float y=event.getY();
        switch (event.getAction()&MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                lastX=x;
                lastY=y;
                return true;
            case MotionEvent.ACTION_UP:
                lastX=x;
                lastY=y;
                return true;
            case MotionEvent.ACTION_MOVE:
                float dy=lastY-y;
                if (dy>10||dy<-10) {
                    xAngle+=dy*factor;
                }
                float dx=lastX-x;
                if (dx>10 || dx<-10) {
                    yAngle+=dx*factor;
                }
                requestRender();
                lastX=x;
                lastY=y;
                return true;
            default:
                break;
        }
        return super.onTouchEvent(event);
    }
    
    private class SceneRenderer implements Renderer{
        
        private float[] cameraMatrix=new float[16];
        private float[] modelMatrix=new float[16];
        private float[] modelViewaMatrix=new float[16];
        private float[] projectionMatrix=new float[16];
        
        private float[] red={1,0,0,1};
        private GLShaderManager shaderManager;
        private GLTriangleBatch torusBatch;

        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
            GLES20.glClearColor(0.3f, 0.3f, 0.3f, 1);
            shaderManager = new GLShaderManager();
            shaderManager.initStockShader();
            
            torusBatch = GLTools.makeTorus(1, 0.3f, 52, 26);
            Matrix.setLookAtM(cameraMatrix, 0, 0, 0, -7, 0, 0, 0, 0, 1, 0);
            GLES20.glEnable(GLES20.GL_DEPTH_TEST);
        }
        
        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {
            GLES20.glViewport(0, 0, width, height);
            GLTools.setPerspective(projectionMatrix, 35, (float)width/(float)height, 1, 500);
        }

        @Override
        public void onDrawFrame(GL10 gl) {
            GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT|GLES20.GL_DEPTH_BUFFER_BIT);
            
            Matrix.setRotateM(modelMatrix, 0, xAngle, 1, 0, 0);
            Matrix.rotateM(modelMatrix, 0, yAngle, 0, 1, 0);
            
            Matrix.multiplyMM(modelViewaMatrix, 0, cameraMatrix, 0, modelMatrix, 0);
            
            shaderManager.useStockShader(ShaderType.DEFAULT_LIGHT, modelViewaMatrix,projectionMatrix,red);
            torusBatch.draw();
        }
    }
}
